#https://www.cnblogs.com/lc1217/p/6514734.html
##最小二乘法
import numpy as np   ##科学计算库 
import scipy as sp   ##在numpy基础上实现的部分算法库

# import matplotlib.pyplot as plt  ##绘图库
# import matplotlib 
# from matplotlib.pylab import mpl  
# mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体  
# mpl.rcParams['axes.unicode_minus'] = False #  解决保存图像是负号'-'显示为方块的问题 


import tushare as ts
ts.set_token("6667cd4a2326f2f937062a0f4fb59aea5c56d13b1f6f26225f115fe9")
pro = ts.pro_api()
#查询当前所有正常上市交易的股票列表
data_1 = pro.stock_basic(exchange='', list_status='L', market="主板",fields='ts_code') #'ts_code,symbol,name,area,industry,list_date')
data_2 = pro.stock_basic(exchange='', list_status='L', market="中小板",fields='ts_code') #'ts_code,symbol,name,area,industry,list_date')  
data=data_1.append(data_2, ignore_index=True) 
print(len(data["ts_code"]))

ts_codes=[]

import datetime
today = datetime.date.today()
start_day=(today-datetime.timedelta(8)).strftime('%Y%m%d') #9日交易日(含休市)数据拟合

for ts_code in data["ts_code"]:
    try:
        df7 = pro.daily(ts_code=ts_code, start_date=start_day)
        change=df7['change'].values #数据日期从近到远
        change_bool=change<0
        # a=np.asarray([1,2,3,4,3,1])
        # b=np.diff(a)
        # c=b<0
        # print(c[:-2].all(),c[-2:].all())
        print(ts_code)
        # print(change_bool[2:],~change_bool[:2])
        if change_bool[2:].all() and (~change_bool[:2]).all():#之前一直跌,近两天回调
            ts_codes.append(ts_code)
            print(ts_code,"is ok!")
    except:
        pass
print(ts_codes)
ts_codes=['000565.SZ', '000570.SZ', '000737.SZ', '000998.SZ', '002075.SZ', '002078.SZ', '002084.SZ', '002092.SZ', '002172.SZ', '002206.SZ', '002258.SZ', '002284.SZ', '002295.SZ', '002395.SZ', '002548.SZ', '002553.SZ', '002597.SZ', '002660.SZ', '002816.SZ', '003043.SZ', '300022.SZ', '300036.SZ', '300121.SZ', '300451.SZ', '300516.SZ', '300679.SZ', '300707.SZ', '300738.SZ', '300806.SZ', '300827.SZ', '300833.SZ', '300948.SZ', '300973.SZ', '600133.SH', '600234.SH', '600255.SH', '600257.SH', '600354.SH', '600362.SH', '600426.SH', '600519.SH', '600520.SH', '600619.SH', '600887.SH', '601001.SH', '601088.SH', '601168.SH', '601699.SH', '601717.SH', '601899.SH', '601965.SH', '603029.SH', '603197.SH', '603301.SH', '603313.SH', '603317.SH', '603380.SH', '603416.SH', '603489.SH', '603616.SH', '603711.SH', '603726.SH', '603809.SH', '603897.SH', '605218.SH', '605338.SH', '605377.SH', '688022.SH', '688038.SH', '688079.SH', '688095.SH', '688313.SH', '688316.SH', '688367.SH', '688395.SH', '688500.SH', '688560.SH', '688589.SH', '688628.SH', '688669.SH', '688799.SH']

import pandas as pd
ts_market=pd.DataFrame()
for i in ts_codes:
    try:
        ts_market=ts_market.append(pro.stock_basic(ts_code=i,fields="market,name,industry,area,list_date"), ignore_index=True)
    except:
        pass
print(ts_market)
ts_market.to_csv("./ts_market1.csv")